<?php

namespace app\admin\model\meet;

use think\Model;


class CouponTemplate extends Model
{


    // 表名
    protected $name = 'meet_coupon_template';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'validity_type_text',
        'start_time_text',
        'end_time_text',
        'status_text',
        'available_type_text',
        'range_type_text'
    ];

    const AVAILABLE_TYPE_ALL = 'all';

    const AVAILABLE_TYPE_MALL = 'mall';

    const AVAILABLE_TYPE_RESERVATION = 'reservation';

    const RANGE_TYPE_ALL = 'all';

    const RANGE_TYPE_APPOINT = 'appoint';


    public function getValidityTypeList()
    {
        return ['0' => __('Validity_type 0'), '1' => __('Validity_type 1')];
    }

    public function getStatusList()
    {
        return ['hidden' => __('Hidden'), 'normal' => __('Normal')];
    }

    public function getAvailableTypeList()
    {
        return ['all' => __('Available_type all'), 'mall' => __('Available_type mall'), 'reservation' => __('Available_type reservation')];
    }

    public function getRangeTypeList()
    {
        return ['all' => __('Range_type all'), 'appoint' => __('Range_type appoint')];
    }

    public function getAvailableTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['available_type']) ? $data['available_type'] : '');
        $list = $this->getAvailableTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRangeTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['range_type']) ? $data['range_type'] : '');
        $list = $this->getRangeTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getValidityTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['validity_type']) ? $data['validity_type'] : '');
        $list = $this->getValidityTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getMoneyAttr($value)
    {
        return $value ? (float)$value : 0;
    }

    public function getLimitAttr($value)
    {
        return $value ? (float)$value : 0;
    }
}
